<?php

class JsonController extends Controller
{

	public function actionCafe()
	{
		if(isset($_GET['id'])){
			$id=intval($_GET['id']);
			$cafe=CoffeeShop::model()->with('imagesAll')->together()->findAllByPk($id);
			if($cafe){
				$images=$cafe[0]->imagesAll;
				foreach($images as $image){
					$image->filePath=Yii::app()->getBaseUrl(true).$image->filePath;
					$image->thumbPath=Yii::app()->getBaseUrl(true).$image->thumbPath;
				}
				$this->jsonHeader();
				$result=array(
					'cafe'=>$cafe[0],
					'images'=>$images
				);
				echo CJSON::encode($result);
			}else{
				throw new CHttpException(404,'Cafe does not exist!');
			}
		}else{
			throw new CHttpException(404,'Missing Parameter');
		}
	}
	public function actionArticle()
	{
		if(isset($_GET['id'])){
			$id=intval($_GET['id']);
			$article=Article::model()->findAllByPk($id);
			if($article){
				$images=array(
					array(
					'id'=>hexdec(substr(md5($article[0]->updated),0,5))%10000,
					'filePath'=>Yii::app()->getBaseUrl(true).$article[0]->filePath,
					'thumbPath'=>Yii::app()->getBaseUrl(true).$article[0]->thumbPath,
				)
				);
				$result=array(
					'article'=>$article[0],
					'images'=>$images
				);
				echo CJSON::encode($result);
			}else{
				throw new CHttpException(404,'Article does not exist!');
			}
		}else{
			throw new CHttpException(404,'Missing Parameter');
		}
	}

	public function actionCafeSearch()
	{
		if(isset($_GET['updatedSince'])){
			$updatedSince=$_GET['updatedSince'];
			$criteria=new CDbCriteria;
			$criteria->compare('timeUpdated',">".$updatedSince,false);
			$criteria->select='id';
			$criteria->order='timeUpdated asc';
			$results=CoffeeShop::model()->findAll($criteria);
			$ids=array();
			foreach($results as $result){
				$ids[]=intval($result->id);
			}
			$this->jsonHeader();
			echo CJSON::encode($ids);
		}else{
			throw new CHttpException(404,'Missing Parameter');
		}
	}
	public function actionArticleSearch()
	{
		if(isset($_GET['updatedSince'])){
			$updatedSince=$_GET['updatedSince'];
			$criteria=new CDbCriteria;
			$criteria->compare('updated',">".$updatedSince,false);
			$criteria->select='id';
			$criteria->order='updated asc';
			$results=Article::model()->findAll($criteria);
			$ids=array();
			foreach($results as $result){
				$ids[]=intval($result->id);
			}
			$this->jsonHeader();
			echo CJSON::encode($ids);
		}else{
			throw new CHttpException(404,'Missing Parameter');
		}
	}

	public function actionIndex()
	{
	}

	protected function jsonHeader(){
		header('Cache-Control: no-cache, must-revalidate');
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
		header('Content-type: application/json');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
	// return the filter configuration for this controller, e.g.:
	return array(
	'inlineFilterName',
	array(
	'class'=>'path.to.FilterClass',
	'propertyName'=>'propertyValue',
	),
	);
	}

	public function actions()
	{
	// return external action classes, e.g.:
	return array(
	'action1'=>'path.to.ActionClass',
	'action2'=>array(
	'class'=>'path.to.AnotherActionClass',
	'propertyName'=>'propertyValue',
	),
	);
	}
	*/
}